﻿<UserControl x:Class="VMClusterManager.Controls.VMListView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:my="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit"
    xmlns:converters="clr-namespace:VMClusterManager.Converters"
             xmlns:local="clr-namespase:VMClusterManager;assembly=VMClusterManager"
             >
    <UserControl.Resources >
        <Style x:Key="DataGridCheckBoxStyle" TargetType="CheckBox" BasedOn="{StaticResource {x:Type CheckBox}}">
            <Setter Property="VerticalAlignment" Value="Center" />
            <Setter Property="Margin" Value="8,0,3,0" />
        </Style>
        <converters:ModelToViewModelConverter x:Key="ModelToViewModel"/>
    </UserControl.Resources>
    <DockPanel>
        
        <Expander Header="Properties" DockPanel.Dock="Bottom" BorderBrush="Black" BorderThickness="1" IsExpanded="{Binding PropertiesExpanded, Mode=TwoWay}">
            <ContentControl Name="VMProperties" Content="{Binding VMProperties}"/>
        </Expander>
        <Expander Name="expVMLog" DockPanel.Dock="Bottom" BorderBrush="Black" BorderThickness="1" Header="Log" IsExpanded="{Binding LogExpanded, Mode=OneWay}" >
            <my:DataGrid ItemsSource="{Binding Log, Mode=OneWay}" AutoGenerateColumns="False">
                <my:DataGrid.Columns>
                    <my:DataGridTemplateColumn>
                        <my:DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <Image Name="messageTypeIcon" Height="16" HorizontalAlignment="Center" VerticalAlignment="Top">
                                </Image>
                                <DataTemplate.Triggers>
                                    <DataTrigger Binding="{Binding MessageType}" Value="0">
                                        <Setter TargetName="messageTypeIcon" Property="Source" Value="{StaticResource ErrorIcon}"/>
                                    </DataTrigger>
                                    <DataTrigger Binding="{Binding MessageType}" Value="1">
                                        <Setter TargetName="messageTypeIcon" Property="Source" Value="{StaticResource WarningIcon}"/>
                                    </DataTrigger>
                                </DataTemplate.Triggers>
                            </DataTemplate> 
                        </my:DataGridTemplateColumn.CellTemplate>
                    </my:DataGridTemplateColumn>
                    <my:DataGridTextColumn Header="Source" Binding="{Binding Source, Mode=OneWay}"/>
                    <my:DataGridTextColumn Header="Message" Binding="{Binding Message, Mode=OneWay}"/>
                    <my:DataGridTextColumn Header="Action" Binding="{Binding Action}"/>
                    <my:DataGridTextColumn Header="Time" Binding="{Binding Time, Mode=OneWay}"/>

                </my:DataGrid.Columns>
            </my:DataGrid>  
        </Expander>
        <Expander Header="Details" DockPanel.Dock="Bottom" BorderBrush="Black" BorderThickness="1" IsExpanded="{Binding DetailsExpanded, Mode=TwoWay}">
            <ContentControl Name="VMDetailsContent" Content="{Binding VMDetails}"/>
        </Expander>

        <Expander Header="Jobs" DockPanel.Dock="Bottom" BorderBrush="Black" BorderThickness="1" IsExpanded="{Binding JobsExpanded, Mode=TwoWay}">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition/>
                    <RowDefinition/>
                </Grid.RowDefinitions>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="Head node name: " VerticalAlignment="Center"/>
                    <TextBox Name="MainNodeName" Text="{Binding MainNodeName, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" MinWidth="150" />
                    <Button Content="Change" MinWidth="60" Command="{Binding ChangeMainNodeNameCommand}" 
                            CommandParameter="{Binding ElementName=MainNodeName, Path=Text}"/>
                </StackPanel>
                <ContentControl Grid.Row="1" Name="JobsView" Content="{Binding HpcJobsView}"/>
            </Grid>
            
        </Expander>

        <Grid>
            <my:DataGrid  ScrollViewer.VerticalScrollBarVisibility="Visible" AutoGenerateColumns="False" 
                         Name="dgVMList" ItemsSource="{Binding VMList, Mode=OneWay, Converter={StaticResource ModelToViewModel}}" 
                          UnloadingRow="dgVMList_UnloadingRow"
                          HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                <my:DataGrid.Columns>
                    <my:DataGridTemplateColumn MinWidth="10" Width="Auto" SortMemberPath="Include">
                        <my:DataGridTemplateColumn.Header>
                            <Menu>
                                <MenuItem Header="Check">
                                    <MenuItem Header="All" Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type my:DataGrid}}, 
                                        Path=DataContext.SelectAllCommand, Mode=OneWay}"
                                        CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type my:DataGrid}}, 
                                        Path=ItemsSource}"
                                              />
                                    <MenuItem Header="None" Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type my:DataGrid}}, 
                                        Path=DataContext.UnselectAllCommand, Mode=OneWay}"
                                        CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type my:DataGrid}}, 
                                        Path=ItemsSource}"/>
                                </MenuItem>
                            </Menu>
                        </my:DataGridTemplateColumn.Header>
                        <my:DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <CheckBox Style="{StaticResource DataGridCheckBoxStyle}" HorizontalAlignment="Center" 
                                           IsChecked="{Binding IsActiveVM}" />
                            </DataTemplate>
                        </my:DataGridTemplateColumn.CellTemplate>
                    </my:DataGridTemplateColumn>
                    <my:DataGridTextColumn 
                    Header="VM"
                    Binding="{Binding VMName}"
                    />
                    <my:DataGridTextColumn Header="Status"
                                           Binding="{Binding StatusString, Mode=OneWay}"/>
                    <my:DataGridTextColumn 
                    Header="Host"
                    Binding="{Binding Path=HostName, Mode=OneWay}"
                    />
                    <my:DataGridTextColumn 
                    Header="Uptime"
                    Binding="{Binding Path=UpTime, Mode=OneWay}"
                    />
                    <my:DataGridTextColumn 
                    Header="CPU"
                    Binding="{Binding Path=ProcessorLoad, Mode=OneWay}"
                    />
                    <my:DataGridTextColumn 
                    Header="Memory"
                    Binding="{Binding Path=MemoryUsage, Mode=OneWay}"
                    />
                    <my:DataGridTextColumn 
                    Header="Created"
                    Binding="{Binding Path=CreationTime, Mode=OneWay}"
                    />
                </my:DataGrid.Columns>
            </my:DataGrid>
        </Grid>
    </DockPanel>

</UserControl>
